package com.java.service.impl.center;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.enums.OrderStatusEnum;
import com.java.common.enums.YseOrNo;
import com.java.common.utils.PageUtils;
import com.java.mapper.OrdersDao;
import com.java.pojo.OrderStatus;
import com.java.pojo.Orders;
import com.java.pojo.vo.MyOrdersVO;
import com.java.pojo.vo.OrderStatusCountsVO;
import com.java.service.OrderStatusService;
import com.java.service.center.MyOrdersService;
import org.aspectj.apache.bcel.generic.RET;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("myOrdersService")
public class MyOrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements MyOrdersService {
    @Autowired
    private OrderStatusService statusService;

    @Override
    public PageUtils queryMyOrders(String userId, Integer status, Integer pageNo, Integer size) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("status", status);
        Page<MyOrdersVO> page = new Page<>(pageNo, size);
        List<MyOrdersVO> list = this.baseMapper.queryMyOrders(page, params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public void updateDeliverOrderStatus(String orderId) {
        OrderStatus updateOrder = new OrderStatus();
        updateOrder.setOrderId(orderId);
        updateOrder.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE.type);
        updateOrder.setDeliverTime(new Date());
        statusService.updateById(updateOrder);
    }

    @Override
    public boolean updateReceiveOrderStatus(String orderId) {
        OrderStatus updateOrder = new OrderStatus();
        updateOrder.setOrderId(orderId);
        updateOrder.setOrderStatus(OrderStatusEnum.SUCCESS.type);
        updateOrder.setDeliverTime(new Date());
        return statusService.updateById(updateOrder);
    }

    @Override
    public boolean deleteOrder(String userId, String orderId) {
        Orders updateOrder = new Orders();
        updateOrder.setIsDelete(YseOrNo.YES.code);
        updateOrder.setUpdatedTime(new Date());
        int update = this.baseMapper.update(updateOrder, new LambdaQueryWrapper<Orders>().eq(Orders::getId, orderId).eq(Orders::getUserId, userId));
        return update == 1;
    }

    @Override
    public OrderStatusCountsVO getOrderStatusCounts(String userId) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("userId", userId);
//        map.put("orderStatus", OrderStatusEnum.WAIT_PAY.type);
//        int waitPayCounts = this.baseMapper.getMyOrderStatusCounts(map);
//        map.put("orderStatus", OrderStatusEnum.WAIT_DELIVER.type);
//        int waitDeliverCounts = this.baseMapper.getMyOrderStatusCounts(map);
//        map.put("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);
//        int waitReceiveCounts = this.baseMapper.getMyOrderStatusCounts(map);
//        map.put("orderStatus", OrderStatusEnum.SUCCESS.type);
//        map.put("isComment", YseOrNo.NO.code);
//        int waitCommentCounts = this.baseMapper.getMyOrderStatusCounts(map);
//        return new OrderStatusCountsVO(waitPayCounts, waitDeliverCounts, waitReceiveCounts, waitCommentCounts);
        return this.baseMapper.getMyOrderStatusCounts1(userId);
    }

    @Override
    public PageUtils getOrdersTrend(String userId, Integer page, Integer pageSize) {
        Page<OrderStatus> pages = new Page<>(page, pageSize);
        List<OrderStatus> list = this.baseMapper.getMyOrderTrend(pages, userId);
        pages.setRecords(list);
        return new PageUtils(pages);
    }
}